package com.zara.store.client.view;

import java.text.NumberFormat;
import java.util.Locale;
import java.util.Observable;
import java.util.logging.Logger;

import com.zara.store.client.clientmodel.OfadModel;
import com.zara.store.client.controller.OfadController;
import com.zara.store.client.view.swing.OfadForm;
import com.zara.store.client.view.swing.table.OfadTableModel;
import com.zara.store.common.ProductVO;

import framework.clientmodel.Model;
import framework.view.View;

public class OfadView extends View {
	
	private OfadForm ofadForm;
	private static Logger LOG = Logger.getLogger(OfadView.class.getName());

	public OfadView(Model mod) {
		super(mod);
		OfadController ofadController = new OfadController(mod, this);
		setController(ofadController);
		ofadForm = new OfadForm(this);
	}

	private NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("es", "AR"));

	public void update(Observable arg0, Object arg1) {
		OfadModel ofadModel = (OfadModel) getModel();
		
		OfadTableModel ofadTable = ofadForm.getOfadTableModel(); //no confundir, es el model de un control swing especifico, estamos actualizando la gui
		OfadTableModel ofadNewTable = ofadForm.getOfadNewTableModel(); //no confundir, es el model de un control swing especifico, estamos actualizando la gui

		ofadNewTable.clear();
		ofadTable.clear();
		
		if(ofadModel.hasOfad()) {
			for(ProductVO p: ofadModel.getItems()) {
				ofadTable.addRow(columnFromVO(p));
			}
			for(ProductVO p: ofadModel.getNewItems()) {
				ofadNewTable.addRow(columnFromVO(p));
			}
		} else {
			LOG.severe("no ofad");
		}
	}

	private Object[] columnFromVO(ProductVO p) {
		String format;
		if(p.getSalePrice()!=null) {
			format = nf.format(p.getSalePrice().doubleValue());
		} else {
			format = "";
		}
		return new Object[]{p.getBarcode(), p.getName(), format, nf.format(p.getUnitPrice().doubleValue())};
	}

	public void display() {
		ofadForm.setVisible(true);
	}

	public OfadForm getOfadForm() {
		return ofadForm;
	}

};
